﻿
using System.Runtime.Serialization;

namespace Tp.Dto.Allgemein
{
	  public class TelefonDto : InPcBase, IAbstractDto
	  {

			#region Members

			private bool isDirty = false;
			private bool canUndo = false;

			private string vorwahl;
			private string vorwahlOld;
			private string telefonNummer;
			private string telefonNummerOld;
			private string durchwahl;
			private string durchwahlOld;

			#endregion

			#region Properties

			[DataMember]
			public int TelefonDtoId { get; set; }

			[DataMember]
			public string Vorwahl {
				  get { return this.vorwahl; }
				  set
				  {
						if (this.vorwahl == value)
						{
							  return;
						}

						if (this.vorwahl != value)
						{
							  this.IsDirty = true;
						}
						this.vorwahlOld = this.vorwahl;
						this.vorwahl = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Vorwahl);
				  }
			}

			[DataMember]
			public string TelefonNummer {
				  get { return this.telefonNummer; }
				  set
				  {
						if (this.telefonNummer == value)
						{
							  return;
						}

						if (this.telefonNummer != value)
						{
							  this.IsDirty = true;
						}
						this.telefonNummerOld = this.telefonNummer;
						this.telefonNummer = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => TelefonNummer);
				
				  }
			}

			[DataMember]
			public string Durchwahl {
				  get { return this.durchwahl; }
				  set
				  {
						if (this.durchwahl == value)
						{
							  return;
						}

						if (this.durchwahl != value)
						{
							  this.IsDirty = true;
						}
						this.durchwahlOld = this.durchwahl;
						this.durchwahl = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Durchwahl);
				  }
			}

			[DataMember]
			public string Rufnummer
			{
				  get {
                    string ret = this.TelefonNummer;
                    if (ret != null && ret.Length > 0) {
                        return this.Vorwahl + " / " + this.TelefonNummer + (this.Durchwahl != null ? "-" + this.Durchwahl : "");
                    }
                    return ret;
                }
				  set
				  {
						string rufnummer = value;
						if (rufnummer.IndexOf("/") > 0)
						{
							  Vorwahl = rufnummer.Substring(0, rufnummer.IndexOf("/") - 1);
							  TelefonNummer = rufnummer.Substring(rufnummer.IndexOf("/")+1);
							  Durchwahl = null;
							  RaisePropertyChanged(() => Rufnummer);
						}
				  }
			}

			public bool HasValue
			{
				  get { return TelefonNummer != null; }
			}

			public bool IsDirty
			{
				  get { return this.isDirty; }
				  set
				  {
						this.isDirty = value;
						RaisePropertyChanged(() => IsDirty);
				  }
			}

			public bool CanUndo
			{
				  get { return this.canUndo; }
				  set
				  {
						this.canUndo = value;
						RaisePropertyChanged(() => CanUndo);
				  }
			}

			#endregion

			#region C-Tor

			public TelefonDto(int telefonId, string vorwahl, string telefonNummer, string durchwahl)
			{
				  this.TelefonDtoId = telefonId;
				  this.vorwahl = vorwahl;
				  this.telefonNummer = telefonNummer;
				  this.durchwahl = durchwahl;
			}

			#endregion

			#region Methods

			public void UndoChanges()
			{
                if (this.CanUndo)
                {
                    this.Vorwahl = (this.vorwahlOld == null ? this.Vorwahl : this.vorwahlOld);
                    this.TelefonNummer = (this.telefonNummerOld == null ? this.TelefonNummer : this.telefonNummerOld);
                    this.Durchwahl = (this.durchwahlOld == null ? this.Durchwahl : this.durchwahlOld);
                    this.CanUndo = false;
                }
			}

			#endregion

			#region Overrides

			public override string ToString()
			{
				  return Rufnummer;
			}
			
			#endregion

	  }
}
